Skip to content

Move proxy values balancing to the beginning of parsing #8133

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 32 commits into
base: main
Choose a base branch
from

Conversation

javorszky
Copy link
Contributor

@javorszky javorszky commented Aug 13, 2025

Proposed changes

This PR moves the proxy values balancing from the template helpers to the time where the annotations and config maps are being parsed. There are a few notable changes:

  • proxy value types (like 4 8k and 16k) are now custom structs with iota constants for the unit (k, m), and uint64 for the numbers. This guarantees that once the values are parsed and corrected, they won't become invalid later through any changes, and we can offload a lot of the error checking to the compiler. One such is that we know none of the values will be negative, because a uint can't be negative
  • some tests had to be changed. For example previously an empty proxy_buffers value passed in via a configmap would generate a bad config, now it gets auto-corrected to the default 2 4k value. This is the cost of moving the proxy balancing to be right after parsing the values
  • similarly the e2e pytest also had to change because if the passed in values are actually invalid, then the result will have those configuration items missing, rather than present, but with invalid values
  • I brought on and reused most of the tests that were in the PR originally, though some of them had to be changed. The logic of what value stays, and what values move around it was fuzzy. Right now if proxy-buffers is not defined, that gets set to the default value of 2 4k, and everything else gets brought down to fit into that one
  • rules of how the values relate to each other are based on the NGINX source code
  • those rules also mean that the proxy_buffers_size is at most all of proxy_buffers minus one buffer size

This PR targets @AlexFenlon 's PR, as it's a modification of that one. Once merged, then we can bring the branch up to date with main.

Questions

Currently I put the custom data type and its tests into the internal/validation package, however there's also the pkg/apis/configuration/v1/types.go file which seems to do something similar. I have lukewarm feelings about where this should live, but the ambiguity around what v1 and v2 mean means that I'm unsure whether I want to put the custom data type in this PR into the pkg/apis/configuration pkg.

Namewise I should probably have named the SizeWithUnit and NumberSizeConfig something more clever. Help there is appreciated.

Tests

Because PRs to non-main and non-release branches don't run CI, I manually ran the unit tests with make test, those passed.

I also manually ran the one failing e2e test with the following after building a fresh image and loading that into minikube:

make run-tests-in-minikube BUILD_IMAGE=nginx/nginx-ingress:5.2.0-SNAPSHOT NODE_IP=$(minikube ip) PYTEST_ARGS='-sv suite/test_virtual_server_configmap_keys.py::TestVirtualServerConfigMapNoTls'

I'm currently running ALL of the e2e tests on a debian image. That might take a while, so I'll return to it once it finished and update this part of the PR message.

Checklist

Before creating a PR, run through this checklist and mark each as complete.

  • I have read the CONTRIBUTING doc
  • I have added tests that prove my fix is effective or that my feature works
  • I have checked that all unit tests pass after adding my changes
  • I have updated necessary documentation
  • I have rebased my branch onto main
  • I will ensure my PR is targeting the main branch and pulling from my branch from my own fork

AlexFenlon and others added 30 commits July 24, 2025 13:35
…at/safe-buffer-config

# Conflicts:
#	internal/configs/commonhelpers/common_template_helpers.go
#	internal/configs/commonhelpers/common_template_helpers_test.go
@javorszky javorszky requested a review from a team as a code owner August 13, 2025 21:32
@github-actions github-actions bot added enhancement Pull requests for new features/feature enhancements go Pull requests that update Go code python Pull requests that update Python code labels Aug 13, 2025
@pdabelf5
Copy link
Collaborator

pkg/apis/configuration/v1/types.go

afaik should only be used for CRD generation

@javorszky javorszky changed the base branch from feat/safe-buffer-config to main August 14, 2025 08:21
Copy link

codecov bot commented Aug 14, 2025

Codecov Report

❌ Patch coverage is 71.31783% with 74 lines in your changes missing coverage. Please review.
✅ Project coverage is 53.33%. Comparing base (d406b39) to head (0186bb5).

Files with missing lines Patch % Lines
internal/configs/annotations.go 16.12% 23 Missing and 3 partials ⚠️
internal/validation/validation.go 60.37% 19 Missing and 2 partials ⚠️
internal/validation/data_types.go 85.60% 14 Missing and 5 partials ⚠️
internal/configs/configmaps.go 77.14% 3 Missing and 5 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #8133      +/-   ##
==========================================
+ Coverage   53.11%   53.33%   +0.21%     
==========================================
  Files          90       91       +1     
  Lines       21778    22024     +246     
==========================================
+ Hits        11567    11746     +179     
- Misses       9736     9790      +54     
- Partials      475      488      +13     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@AlexFenlon AlexFenlon mentioned this pull request Aug 14, 2025
6 tasks
@AlexFenlon AlexFenlon linked an issue Aug 14, 2025 that may be closed by this pull request
5 tasks
@AlexFenlon
Copy link
Contributor

Docs PR: nginx/documentation#917

@danielnginx danielnginx requested a review from Copilot August 14, 2025 12:58
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR moves proxy buffer value balancing from template helpers to the config/annotation parsing phase. It introduces custom data types for size and buffer configurations to ensure type safety and consistency, along with validation rules based on NGINX source code requirements.

Key changes:

  • Introduces SizeWithUnit and NumberSizeConfig structs with type-safe parsing and validation
  • Implements BalanceProxyValues function to normalize proxy buffer relationships according to NGINX constraints
  • Updates test expectations to handle auto-correction of invalid proxy buffer configurations

Reviewed Changes

Copilot reviewed 28 out of 28 changed files in this pull request and generated 6 comments.

Show a summary per file
File Description
internal/validation/data_types.go Defines new custom types for size and buffer configurations with validation logic
internal/configs/configmaps.go Updates config parsing to use new types and balance proxy values
internal/configs/annotations.go Updates annotation parsing to use new types and balance proxy values
tests/suite/test_virtual_server_configmap_keys.py Updates test to handle corrected proxy buffer behavior
internal/configs/virtualserver.go Updates template generation to use string representation of new types

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

}

// Autocorrect invalid units to 'm'
if unit != 'k' && unit != 'm' {
Copy link
Preview

Copilot AI Aug 14, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The autocorrection logic that changes invalid units to 'm' is not documented in the function comment. This behavior should be clearly documented as it could be unexpected for users.

Copilot uses AI. Check for mistakes.

}

if num > 1024 {
ret.Size = 1024
Copy link
Preview

Copilot AI Aug 14, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The magic number 1024 should be defined as a named constant to improve code readability and maintainability.

Suggested change
ret.Size = 1024
if num > MaxAllowedSize {
ret.Size = MaxAllowedSize

Copilot uses AI. Check for mistakes.

}

if num < 2 {
num = 2
Copy link
Preview

Copilot AI Aug 14, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The magic number 2 (minimum number of buffers) should be defined as a named constant to improve code readability and document the NGINX requirement.

Suggested change
num = 2
if num < MinNginxBufferCount {
num = MinNginxBufferCount

Copilot uses AI. Check for mistakes.

// 1.b there must be at most 1024 proxy buffers
if proxyBuffers.Number > 1024 {
modifications = append(modifications, fmt.Sprintf("adjusted proxy_buffers number from %d to 1024", proxyBuffers.Number))
proxyBuffers.Number = 1024
Copy link
Preview

Copilot AI Aug 14, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The magic number 1024 appears again and should be defined as a named constant to avoid duplication and improve maintainability.

Suggested change
proxyBuffers.Number = 1024
// 1.b there must be at most MaxProxyBuffers proxy buffers
if proxyBuffers.Number > MaxProxyBuffers {
modifications = append(modifications, fmt.Sprintf("adjusted proxy_buffers number from %d to %d", proxyBuffers.Number, MaxProxyBuffers))
proxyBuffers.Number = MaxProxyBuffers

Copilot uses AI. Check for mistakes.

} else {
cfgParams.ProxyBufferSize = proxyBufferSizeData
}
// normalizedProxyBufferSize := validation.NormalizeBufferSize(proxyBufferSize)
Copy link
Preview

Copilot AI Aug 14, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This commented out code should be removed as it's not being used and adds clutter to the codebase.

Suggested change
// normalizedProxyBufferSize := validation.NormalizeBufferSize(proxyBufferSize)

Copilot uses AI. Check for mistakes.

}
}

// Normalise the three proxy buffer values across each other.
Copy link
Preview

Copilot AI Aug 14, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment doesn't add value as it just restates what the code above does. It should be removed or made more descriptive.

Suggested change
// Normalise the three proxy buffer values across each other.
// Ensure proxy_buffers, proxy_buffer_size, and proxy_busy_buffers_size are consistent.
// The BalanceProxyValues function reconciles these values to prevent misconfiguration,
// applying necessary adjustments and logging any changes made.

Copilot uses AI. Check for mistakes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Pull requests for new features/feature enhancements go Pull requests that update Go code python Pull requests that update Python code
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Safe proxy buffer configuration
3 participants